package tuillayer;
import java.util.InputMismatchException;
import java.util.Scanner;
import controllayer.*;

/**
 * Textbased UI which allows the user to input information about persons.
 */
public class PersonUI {

	// instance variables - object by the type PersonCtrl
	/** The p ctrl. */
	LoanCtrl lCtrl = new LoanCtrl();
	
	/** The p ctrl. */
	private PersonCtrl pCtrl;

	/** The instance. */
	private static PersonUI instance;

	/**
	 * Constructor for PersonUI.
	 */
	public PersonUI()
	{
		// Initialize pCtrl
		pCtrl = new PersonCtrl();
	}

	/**
	 * Gets the single instance of PersonUI.
	 *
	 * @return single instance of PersonUI
	 */
	public static PersonUI getInstance()
	{
		if(instance == null)
		{
			instance = new PersonUI();
		}
		return instance;
	}

	/**
	 * Person menu.
	 */
	public void personMenu()
	{
		boolean exit = false;
		try {
			while(!exit)//that is: while exit is false
			{
				int choice = writepersonMenu(); 
				if(choice == 1)
				{   //Create person

					pCtrl.createPerson(inputName(), inputAddress(), inputPostal(), inputCity(), inputPhone());
				}
				else if(choice == 2)
				{ //find person
					pCtrl.printPersonInfo(pCtrl.findPerson(inputPhone()));
				}
				else if(choice == 3)
				{
					System.out.println("\nInput phone number for the person you will like to edit:");
					String currentPhone = inputPhone();
					if(pCtrl.findPerson(currentPhone) !=null)
					{
						System.out.println("\nEnter new information");
						String name = inputName();        
						String address = inputAddress();
						String zip = inputPostal();
						String city = inputCity();
						String phone = inputPhone();
						pCtrl.editPerson(currentPhone, name, address, zip, city, phone);
						System.out.println("\nFriend succesfully updated");
					}
					else
					{
						System.out.println("\nFriend not found. I'm sorry about that.");
					}
				}

				else if(choice == 4)
				{
					pCtrl.deletePerson(inputPhone());
				}
				else
				{
					exit = true;
					// }//end else
				}//end else

			}//end while
		}
		catch (InputMismatchException e) 
		{
			//Prints appropriate error message
			System.out.println("Du skal indtaste et tal!");
			personMenu();
		}
	}

	/**
	 * Writeperson menu.
	 *
	 * @return the int
	 */
	private int writepersonMenu()
	{      // makes an object keyboard to read input from the screen
		Scanner keyboard = new Scanner(System.in);
		System.out.println("*** Friends Menu ***");
		System.out.println(" (1) Add a friend");
		System.out.println(" (2) Find a friend");
		System.out.println(" (3) Edit a friend");
		System.out.println(" (4) Delete a friend");
		System.out.println(" (5) Exit");
		System.out.print("\n Choose: ");

		int choice = keyboard.nextInt();
		return choice;
	}
	/**
	 * Input name.
	 *
	 * @return the string
	 */
	private String inputName()
	{
		// makes an object keyboard to have input from the screen
		Scanner keyboard = new Scanner(System.in);  
		System.out.println(" Name:  ");
		String name = keyboard.nextLine();
		//      
		return name;
	}

	/**
	 * Input address.
	 *
	 * @return the string
	 */
	private String  inputAddress()
	{
		// makes an object keyboard to read input from the console
		Scanner keyboard = new Scanner(System.in);
		System.out.println(" Street: ");
		String address = keyboard.nextLine();

		return address;
	}

	/**
	 * Input postal.
	 *
	 * @return the string
	 */
	private String  inputPostal()
	{
		// makes an object keyboard to read input from the console
		Scanner keyboard = new Scanner(System.in);
		System.out.println(" Postal: ");
		String postal = keyboard.nextLine();

		return postal;
	}

	/**
	 * Input city.
	 *
	 * @return the string
	 */
	private String  inputCity()
	{
		// makes an object keyboard to read input from the console
		Scanner keyboard = new Scanner(System.in);
		System.out.println(" City: ");
		String city = keyboard.nextLine();
		return city;
	}

	/**
	 * Input phone.
	 *
	 * @return the string
	 */
	public String  inputPhone()
	{
		// makes an object keyboard to read input from the console
		Scanner keyboard = new Scanner(System.in);
		System.out.println(" Phone: ");
		String phone = keyboard.nextLine();
		return phone;
	}

}
